package cn.zjxiliu.lib_core.utils;

import android.app.Application;

import java.lang.reflect.Field;

/**
 * Created by jessie on 2018/10/25.
 */

public class ResourceUtils {

    public static int getLayoutId(Application context, String paramString) {
        if (context == null)
            return 0;
        return context.getResources().getIdentifier(paramString, "layout", context.getPackageName());
    }

    public static int getStringId(Application context, String paramString) {
        if (context == null)
            return 0;
        return context.getResources().getIdentifier(paramString, "string",
                context.getPackageName());
    }

    public static int getDrawableId(Application context, String paramString) {
        if (context == null)
            return 0;
        return context.getResources().getIdentifier(paramString, "drawable", context.getPackageName());
    }

    public static int getStyleId(Application context, String paramString) {
        if (context == null)
            return 0;
        return context.getResources().getIdentifier(paramString, "style",
                context.getPackageName());
    }

    public static int getId(Application context, String paramString) {
        if (context == null)
            return 0;
        return context.getResources().getIdentifier(paramString, "id",
                context.getPackageName());
    }

    public static int getColorId(Application context, String paramString) {
        if (context == null)
            return 0;
        return context.getResources().getIdentifier(paramString, "color", context.getPackageName());
    }

    public static int getDimenId(Application context, String paramString) {
        if (context == null)
            return 0;
        return context.getResources().getIdentifier(paramString, "dimen",
                context.getPackageName());
    }

    public static int getAnimId(Application context, String paramString) {
        if (context == null)
            return 0;
        return context.getResources().getIdentifier(paramString, "anim", context.getPackageName());
    }

    // 通过反射实现
    public static final int[] getStyleableIntArray(Application context, String name) {
        try {
            if (context == null)
                return null;
            Field field = Class.forName(context.getPackageName() + ".R$styleable").getDeclaredField(name);
            int[] ret = (int[]) field.get(null);
            return ret;
        } catch (Throwable t) {
        }
        return null;
    }

    public static final int getStyleableIntArrayIndex(Application context, String name) {
        try {
            if (context == null)
                return 0;
            // use reflection to access the resource class
            Field field = Class.forName(context.getPackageName() + ".R$styleable").getDeclaredField(name);
            int ret = (Integer) field.get(null);
            return ret;
        } catch (Throwable t) {
        }
        return 0;
    }



}
